//
//  BaseTextView.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2019/12/6.
//  Copyright © 2019 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

class BaseTextView: NSView {
    
    let lineHeightMultiple: CGFloat = 1.6
    var font: NSFont = NSFont.systemFont(ofSize: NSFont.systemFontSize)
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setupUI()
    }
    
    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        self.addSubview(scrolleView)
        scrolleView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        scrolleView.documentView = txView
        txView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    lazy var scrolleView: NSScrollView = {
        let view = NSScrollView.init(frame: CGRect.zero)
        view.hasVerticalRuler = false
        view.hasHorizontalRuler = false
        view.automaticallyAdjustsContentInsets = false
        view.hasVerticalScroller = false
        view.horizontalScrollElasticity = .automatic
        view.verticalScrollElasticity = .automatic
        view.backgroundColor = NSColor.clear
        return view
    }()
    
    lazy var txView: NSTextView = {
        let view = NSTextView()
        view.minSize = NSMakeSize(0, 100)
        view.maxSize = NSMakeSize(CGFloat(Float.greatestFiniteMagnitude), CGFloat(Float.greatestFiniteMagnitude))
        view.isEditable = false
        view.isVerticallyResizable = true
        view.isHorizontallyResizable = false
        view.backgroundColor = NSColor.clear
        view.string = ""
        view.font = NSFont.systemFont(ofSize: 16)
        view.textColor = color_gray(200)
        view.sizeToFit()
        view.layoutManager?.delegate = self
        view.textContainer?.containerSize = NSMakeSize(CGFloat(Float.greatestFiniteMagnitude), CGFloat(Float.greatestFiniteMagnitude))
        view.textContainer?.widthTracksTextView = true
        view.textContainer?.maximumNumberOfLines = 2
        view.invalidateIntrinsicContentSize()
        return view
    }()
    
    
}

extension BaseTextView:NSLayoutManagerDelegate {
    
    
    func layoutManager(_ layoutManager: NSLayoutManager, shouldSetLineFragmentRect lineFragmentRect: UnsafeMutablePointer<NSRect>, lineFragmentUsedRect: UnsafeMutablePointer<NSRect>, baselineOffset: UnsafeMutablePointer<CGFloat>, in textContainer: NSTextContainer, forGlyphRange glyphRange: NSRange) -> Bool {
        let fontLineHeight = layoutManager.defaultLineHeight(for: font)
        let lineHeight = fontLineHeight * lineHeightMultiple
        let baselineNudge = (lineHeight - fontLineHeight)
            // The following factor is a result of experimentation:
            * 0.6

        var rect = lineFragmentRect.pointee
        rect.size.height = lineHeight

        var usedRect = lineFragmentUsedRect.pointee
        usedRect.size.height = max(lineHeight, usedRect.size.height) // keep emoji sizes

        lineFragmentRect.pointee = rect
        lineFragmentUsedRect.pointee = usedRect
        baselineOffset.pointee = baselineOffset.pointee + baselineNudge

        return true
    }
}
extension NSScrollView {

    func scrollToTop() {
        if let documentView = self.documentView {
            if documentView.isFlipped {
                documentView.scroll(.zero)
            } else {
                let maxHeight = max(bounds.height, documentView.bounds.height)
                documentView.scroll(NSPoint(x: 0, y: maxHeight))
            }
        }
    }
    
    func scrollToBottom() {
        if let documentView = self.documentView {
            if documentView.isFlipped {
                let maxHeight = max(bounds.height, documentView.bounds.height)
                documentView.scroll(NSPoint(x: 0, y: maxHeight))
            } else {
                let maxHeight = max(bounds.height, documentView.bounds.height)
                documentView.scroll(NSPoint(x: 0, y: maxHeight))
            }
        }
    }
    
}
